package com.jeff.android.lab.jetpack.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;

/**
 * @author: dingfeng
 * @date: 2021/3/3
 * @since: 1.0.0
 **/
public abstract class LazyFragmentX extends Fragment {

    protected Context mContext;

    protected abstract int getRootLayout();

    private boolean isLazyLoaded = false;

    protected void onFragmentVisibleChange(boolean visible) {
    }

    protected void initView() {
    }

    protected void initData() {
    }

    protected void onLazyLoad() {
        // 加载数据
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getRootLayout(), container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isLazyLoaded) {
            this.isLazyLoaded = true;
            onLazyLoad();
        }
        onFragmentVisibleChange(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        onFragmentVisibleChange(false);
    }

}
